package gen

import (
	"strings"
)

type IfBlock struct {
	CodeBlock
	ifNum int
}

func OfIfBlock(head string) ICodeBlock {
	var ifBlock IfBlock
	ifBlock.Head = head
	ifBlock.ifNum = 1
	return &ifBlock
}

func (i *IfBlock) Build() {
	heads := strings.Split(i.Head, " ")
	ok := GetFromExpression(heads[1]).(bool)
	if !ok {
		return
	}
	build0(i)
}

func (i *IfBlock) isEnd() bool {
	return i.ifNum <= 0
}

func (i *IfBlock) checkStep(line string) {
	if !strings.HasPrefix(line, "#if") {
		return
	}
	if strings.Compare(line, "#if end") == 0 {
		i.ifNum -= 1
	} else {
		i.ifNum += 1
	}
}

// 添加行表达式
func (i *IfBlock) addLineExpression(lineExpression string) {
	i.buildedLines = append(i.buildedLines, ParseFromExpression((lineExpression)))
}

func (i *IfBlock) GetDesc() string {
	return "IfBlock at " + i.Head
}
